package com.mmy.controller.hotel;

import java.math.BigDecimal;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.mmy.domain.hotel.THotelOrder;
import com.mmy.domain.hotel.VHotelPlan;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.hotel.HotelOrderServiceImpl;
import com.mmy.service.impl.hotel.HotelPlanServiceImpl;

/**
 * 类的介绍：酒店订单
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */

@Controller
public class HotelOrderController {

	@Autowired
	HotelOrderServiceImpl hotelOrderServiceImpl;
	@Autowired
	HotelPlanServiceImpl hotelPlanServiceImpl;

	/**
	 * 函数功能说明： 前台个人中心点击取消房间预留
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping(value = "/enterroomcancelreserve")
	public String enterRoomCancelReserve(HttpServletRequest request, HttpSession session) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		hotelOrderServiceImpl.updateState(iID, sessionSysUser);
		return "redirect:myhotelreserve";

	}

	/**
	 * 函数功能说明： 前台个人中心点击申请酒店退单
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping(value = "/enterroomapplyrefund")
	public String enterRoomApplyRefund(HttpServletRequest request, HttpSession session) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		hotelOrderServiceImpl.updateRefundState(iID, sessionSysUser);
		return "redirect:myhotelorder";
	}

	/**
	 * 函数功能说明： 前台个人中心点击撤销房间退单
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping(value = "/enterroomcancelrefund")
	public String enterRoomCancelRefund(HttpServletRequest request, HttpSession session) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		hotelOrderServiceImpl.updateCancelState(iID, sessionSysUser);
		return "redirect:myhotelrefund";
	}

	/* 前台个人中心显示订单详情 */
	@RequestMapping(value = "/myhotelinfo")
	public String toMyHotelInfo(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		THotelOrder dbHotelOrder = hotelOrderServiceImpl.findByID(iID);
		model.addAttribute("dbHotelOrder", dbHotelOrder);

		String strOrderID = dbHotelOrder.getOrderID();
		int iPlanID = Integer.parseInt(strOrderID.substring(9, 18));

		VHotelPlan dbHotelPlan = hotelPlanServiceImpl.findByPlanID(iPlanID);

		model.addAttribute("dbHotelPlan", dbHotelPlan);

		return "permanagement/MyHotelInfo";
	}

	/**
	 * 函数功能说明：前台 新建酒店预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人hanxl
	 * @date 创建日期 2021.03.02
	 */
	@RequestMapping(value = "/enterreservehotel")
	public String EnterReserveHotel(HttpServletRequest request, Model model) {
		// 获取传入的房间ID
		String strRoomID = request.getParameter("iRoomID");
		// 获取该酒店在计划内的房间
		List<Map<String, Object>> lstHotelPlanRoom = hotelPlanServiceImpl
				.findPlanValidRoomID(request.getParameter("iHotelID"));
		model.addAttribute("lstHotelPlanRoom", lstHotelPlanRoom);
		model.addAttribute("iHotelID", request.getParameter("iHotelID"));
		// 查询该景区时间在计划日期内的计划

		if (strRoomID != null) {
			List<VHotelPlan> lstHotelPlan = hotelPlanServiceImpl.findPlanValid(strRoomID);
			model.addAttribute("lstHotelPlan", lstHotelPlan);

			Format f = new SimpleDateFormat("yyyy-MM-dd");
			Calendar cal = Calendar.getInstance();
			cal.setTime(new Date());
			List<Map<String, String>> list = new ArrayList<>();
			for (int i = 0; i < 90; i++) {
				cal.add(Calendar.DAY_OF_MONTH, 1);
				cal.set(Calendar.HOUR_OF_DAY, 0);
				cal.set(Calendar.MINUTE, 0);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);
				Date today = cal.getTime();
				Map<String, String> map = new HashMap<>();
				map.put("date", f.format(today));
				List<BigDecimal> lstPrice = new ArrayList<BigDecimal>();
				for (VHotelPlan vHotelPlan : lstHotelPlan) {
					Date startDate = vHotelPlan.getStartDate();
					Date endDate = vHotelPlan.getEndDate();
					if (today.before(endDate) & startDate.before(today)) {
						BigDecimal price = vHotelPlan.getPrice();
						lstPrice.add(price);
					}
				}

				if (lstPrice.size() == 0) {
					continue;
				} else {
					BigDecimal min = Collections.min(lstPrice);
					map.put("price", min.toString());
					list.add(map);
				}

			}
			model.addAttribute("strRoomID", strRoomID);
			model.addAttribute("json", JSONObject.toJSONString(list));
		}
		return "hotel/ReserveHotel";

	}

	/**
	 * 函数功能说明： 新建酒店订单信息提交
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.02
	 */
	@ResponseBody
	@RequestMapping(value = "/savereservehotel")
	public Map<String, Object> saveReserveHotel(@RequestParam("plan") String strHotelPlan,
			@RequestParam("order") String strHotelOrder, @RequestParam("useDate") String strUseDate,
			HttpSession session, Model model) {

		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		Map<String, Object> data = hotelOrderServiceImpl.createOrderHotel(strHotelPlan, strHotelOrder, strUseDate,
				sessionSysUser);

		return data;
	}

	/**
	 * 函数功能说明： 新建酒店订单信息提交 进入填写收据页面
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.02
	 */
	@RequestMapping(value = "/topayorderhotel")
	public String toPayOrderHotel(HttpServletRequest request, Model model) {
		String lstID = request.getParameter("lstID");
		// 拆分字符串
		String[] arrID = lstID.split(",");
		THotelOrder dbHotelOrder = hotelOrderServiceImpl.findByID(Integer.parseInt(arrID[0]));
		model.addAttribute("iOrderAmount", dbHotelOrder.getOrderAmount());
		model.addAttribute("lstID", lstID);
		return "hotel/PayOrderHotel";
	}

	/**
	 * 函数功能说明：收据提交
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 2021.01.12
	 */
	@RequestMapping(value = "/savepayorderhotel")
	public String savePayOrderHotel(THotelOrder tHotelOrder, HttpSession session, String lstID, Model model) {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		List<THotelOrder> lstHotelOrder = hotelOrderServiceImpl.updateHotelOrder(tHotelOrder, sessionSysUser, lstID);

		model.addAttribute("lstID", lstID);
		model.addAttribute("tHotelOrder", lstHotelOrder.get(0));
		model.addAttribute("lstHotelOrder", lstHotelOrder);
		return "hotel/PrintHotelOrder";

	}

}
